﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Web;
using eBlog.Common.Files;

namespace eBlog.Common.Security
{
    public class MD5
    {
        public static string Encrypt(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }

        public static string ADD(string str)
        {
            return Encrypt(str + "");
        }

        public static string FormatCookie(string name)
        {
            string ip =  HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            string agent = HttpContext.Current.Request.UserAgent;
            string key = ConfigHelper.GetConfigString("SiteKey");
            name = HttpContext.Current.Server.UrlDecode(name);
            string c_key = FormsAuthentication.HashPasswordForStoringInConfigFile(ip + agent + key + name, "MD5");
            return c_key;
        }

        public static string GetMD5(string content)
        {
            lock (typeof(MD5))
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(content, "md5");
            }
        }

        public static byte[] GetMD5(byte[] content)
        {
            lock (typeof(MD5))
            {
                return StringHelper.StringToByte(GetMD5(StringHelper.ByteToString(content)));
            }
        }

        public static bool Compare(string contentA, string contentB)
        {
            if (GetMD5(contentA).Trim().ToUpper() == GetMD5(contentB).ToUpper().Trim())
                return true;
            return false;
        }

        public static bool Compare(byte[] contentA, byte[] contentB)
        {
            return Compare(StringHelper.ByteToString(contentA), StringHelper.ByteToString(contentB));
        }

    }

}
